package top.lcm668.study.oj.base.sortmethods;

/**
 * @author: Coder-Lcm
 * @date: 2021/10/5 5:34
 * 冒泡排序
 */
public class BubbleSort<T extends Comparable<T>> extends BaseSort<T> {

    @Override
    protected void sort(T[] arr, boolean isAsc) {
        int len = arr.length;
        boolean sorted = false;
        for (int i = len - 1; i > 0 && !sorted; i--) {
            sorted = true;
            for (int i1 = 0; i1 < i ; i1++) {
                if (isAsc ? lessThan(arr[i1 + 1], arr[i1]) : largeThan(arr[i1 + 1], arr[i1])) {
                    sorted = false;
                    swap(arr, i1, i1 + 1);
                }
            }
        }
    }
}

